package com.gamecollection.fish.model;

import com.gamecollection.mvc.Model;
import com.gamecollection.fish.model.obj.Bg;
import com.gamecollection.fish.model.obj.Enamy;
import com.gamecollection.fish.model.obj.MyFish;

import java.util.ArrayList;
import java.util.List;

/**
 * 大鱼吃小鱼游戏模型
 */
public class FishModel implements Model {
    
    // 游戏状态常量
    public static final int STATE_NOT_STARTED = 0;
    public static final int STATE_PLAYING = 1;
    public static final int STATE_GAME_OVER = 2;
    public static final int STATE_WIN = 3;
    public static final int STATE_PAUSED = 4;
    
    private int gameState = STATE_NOT_STARTED;
    private int time = 0;
    private int level = 0;
    private int count = 0;
    
    // 背景
    private Bg bg;
    
    // 敌方鱼类
    private List<Enamy> enamyList;
    
    // boss
    private Enamy boss;
    
    // 是否生成boss
    private boolean isBoss = false;
    
    // 我方鱼类
    private MyFish myFish;
    
    // 游戏区域尺寸
    private int width = 1440;
    private int height = 900;
    
    public FishModel() {
        enamyList = new ArrayList<>();
        bg = new Bg();
        myFish = new MyFish();
    }
    
    @Override
    public void initialize() {
        gameState = STATE_NOT_STARTED;
        time = 0;
        level = 0;
        count = 0;
        enamyList.clear();
        isBoss = false;
        boss = null;
        myFish = new MyFish();
        // 设置玩家初始等级为2，提升初始能力
        myFish.setLevel(2);
    }
    
    @Override
    public void update() {
        if (gameState == STATE_PLAYING) {
            time++;
            
            // 更新背景
            bg.update();
            
            // 更新我方鱼类
            myFish.update();
            
            // 更新敌方鱼类
            for (Enamy enamy : enamyList) {
                enamy.update();
            }
            
            // 更新boss
            if (isBoss && boss != null) {
                boss.update();
            }
            
            // 游戏逻辑
            updateGameLogic();
            
            // 检查碰撞
            checkCollisions();
        }
    }
    
    @Override
    public void reset() {
        initialize();
    }
    
    @Override
    public int getGameState() {
        return gameState;
    }
    
    @Override
    public void setGameState(int state) {
        this.gameState = state;
    }
    
    /**
     * 更新游戏逻辑
     */
    private void updateGameLogic() {
        // 关卡难度
        if (count < 5) {
            level = 0;
            myFish.setLevel(2); // 初始等级为2
        } else if (count <= 15) {
            level = 1;
            myFish.setLevel(2);
        } else if (count <= 50) {
            level = 2;
            myFish.setLevel(3);
        } else if (count <= 150) {
            level = 3;
            myFish.setLevel(3);
        } else if (count <= 300) {
            level = 4;
            myFish.setLevel(3);
        } else {
            gameState = STATE_WIN;
            return;
        }
        
        // 生成敌方鱼类
        generateEnamy();
        
        // 生成boss
        generateBoss();
    }
    
    /**
     * 生成敌方鱼类
     */
    private void generateEnamy() {
        // 降低生成频率：从每50帧改为每80帧
        if (time % 5 == 0) {
            Enamy enamy = new Enamy();
            // 降低敌鱼速度
            enamy.setSpeed(enamy.getSpeed() / 2);
            enamyList.add(enamy);
        }
    }
    
    /**
     * 生成boss
     */
    private void generateBoss() {
        // 延迟boss出现：从100分改为150分
        if (count >= 150 && !isBoss) {
            boss = new Enamy();
            boss.setBoss(true);
            // 降低boss速度
            boss.setSpeed(boss.getSpeed() / 2);
            isBoss = true;
        }
    }
    
    /**
     * 检查碰撞
     */
    private void checkCollisions() {
        // 检查我方鱼类与敌方鱼类的碰撞
        for (int i = enamyList.size() - 1; i >= 0; i--) {
            Enamy enamy = enamyList.get(i);
            if (myFish.getX() < enamy.getX() + enamy.getWidth() &&
                myFish.getX() + myFish.getWidth() > enamy.getX() &&
                myFish.getY() < enamy.getY() + enamy.getHeight() &&
                myFish.getY() + myFish.getHeight() > enamy.getY()) {
                
                if (myFish.getLevel() >= enamy.getLevel()) {
                    // 吃掉敌方鱼类
                    enamyList.remove(i);
                    count++;
                } else {
                    // 游戏结束
                    gameState = STATE_GAME_OVER;
                }
            }
        }
        
        // 检查与boss的碰撞
        if (isBoss && boss != null) {
            if (myFish.getX() < boss.getX() + boss.getWidth() &&
                myFish.getX() + myFish.getWidth() > boss.getX() &&
                myFish.getY() < boss.getY() + boss.getHeight() &&
                myFish.getY() + myFish.getHeight() > boss.getY()) {
                
                if (myFish.getLevel() >= boss.getLevel()) {
                    // 吃掉boss
                    boss = null;
                    isBoss = false;
                    count += 10;
                } else {
                    // 游戏结束
                    gameState = STATE_GAME_OVER;
                }
            }
        }
    }
    
    // Getter方法
    public Bg getBg() {
        return bg;
    }
    
    public List<Enamy> getEnamyList() {
        return enamyList;
    }
    
    public Enamy getBoss() {
        return boss;
    }
    
    public boolean isBoss() {
        return isBoss;
    }
    
    public MyFish getMyFish() {
        return myFish;
    }
    
    public int getTime() {
        return time;
    }
    
    public int getLevel() {
        return level;
    }
    
    public int getCount() {
        return count;
    }
    
    public int getWidth() {
        return width;
    }
    
    public int getHeight() {
        return height;
    }
} 